
import { computed, ref } from 'vue';
import { updateEntity, deleteEntity } from '@/api/dashboard/perm';
import { getDict } from '@/api/base';
import type { SelectOptionData } from '@arco-design/web-vue/es/select/interface';
import { Modal, Message } from '@arco-design/web-vue';
import { validateForm } from '@/utils';

export let modalType = ref(0);
export let currRecord = ref({} as any);

export let parType = ref([] as SelectOptionData[]);
export let parType2 = ref([] as SelectOptionData[]);
export let accessTypes = ref([] as SelectOptionData[]);

(async function () {
  let data = await getDict(10, 'PermissionTypes');
  parType.value = data;
  
  let data2 = await getDict(10, 'ScopeTypes');
  parType2.value = data2;

  let data3 = await getDict(10, 'NONE');
  accessTypes.value = data3;
})();

export function addNew () {
  modalType.value = 3;
  currRecord.value = {
    enabled: true
  };
  validateForm(null, true);
}
export function addNext() {
  if(!currSelect) {
    Message.warning('请先选择一行');
    return;
  }
  modalType.value = 3;
  currRecord.value = {
    applicationId: currSelect.applicationId,
    parentId: currSelect.permissionId,
    parentName: currSelect.permissionName,
    permissionLevel: currSelect.permissionLevel + 1,
    enabled: true
  };
  validateForm(null, true);
}
let currSelect:any;
export function rowSelect(rowKeys:Array<string>, key:string, record:any) {
  currSelect = record;
}

export function btnView (record:any) {
  modalType.value = 1;
  currRecord.value = record;
}

export function btnEdit (record:any) {
  modalType.value = 2;
  currRecord.value = {...record};
  validateForm(null, true);
}

export function btnDelete (record:any, call:Function) {
  Modal.confirm({
    title: '确认',
    content: `确定要删除【${record.permissionName}】吗？`,
    onOk: async function() {
      await deleteEntity(record.permissionId);
      call();
    }
  });
}

export async function btnEditSave (call:Function) {
  if(!validateForm()) {
    return;
  }
  modalType.value = 0;
  await updateEntity(currRecord.value);
  call();
}